package qc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.quickconnect.ControlObject;
import org.quickconnect.QuickConnect;

import ui.MainFrame;

import com.mysql.jdbc.Connection;

import controller.ServerConnectionHandler;

public class SessionHasModeratorValCO implements ControlObject{

	@Override
	public Object handleIt(ArrayList<Object> arg0) {
		ServerConnectionHandler connection = (ServerConnectionHandler) arg0.get(0);
		HashMap map = (HashMap) arg0.get(arg0.size()-1);
		String sessionId = (String) map.get("sessionId");

		Connection con = (Connection)MainFrame.mainFrame.getController().getConnectionPool().getConnection();
		java.sql.PreparedStatement select = null;
		ResultSet results = null;
		try {
			select = con.prepareStatement("SELECT * FROM Moderator m INNER JOIN SessionParticipants sp ON m.userid = sp.userid AND m.sessionID = sp.sessionID WHERE m.sessionID = (SELECT SessionID FROM Session WHERE SessionNumber = ?)");
			select.setString(1, sessionId);
			results = select.executeQuery();
			if (results.next()) 
			{
				MainFrame.mainFrame.getController().getConnectionPool().returnConnection(con);
				return true;
			}
			MainFrame.mainFrame.getController().getConnectionPool().returnConnection(con);
			ArrayList errors = new ArrayList();
			HashMap errorsMap = new HashMap();
			errorsMap.put("message", "Session has no moderators.");
			errors.add(connection);
			errors.add(errorsMap);
			System.out.println("Sending Error");
			QuickConnect.handleError("sendError", errors);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return false;
	}
}
